﻿@page "/votes/createvote"
@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService
@inject IPopupService PopupService

@inject ILocalStorageService _localStorage


@inject NavigationManager NavigationManager

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="创建投票"/>

<MakeSureInWASMTip></MakeSureInWASMTip>


<div>
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="创建投票" Icon="@IconType.Vote.ToIconString()" Outline>
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Votes.MainCard Model="Model" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Votes.OptionsCard Model="Model" Class="mb-4"/>
         <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Votes.RelevancesCard Model="Model" Class="mb-4" EntryGameItems="EntryGameItems" EntryRoleItems="EntryRoleItems" EntryGroupItems="EntryGroupItems" PeripheryItems="PeripheryItems" EntryStaffItems="EntryStaffItems" ArticleItems="ArticleItems" />
         <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Votes.MainPageCard Model="Model" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
    }

</div>


@code {
    private EditVoteModel Model { get; set; } = new EditVoteModel
    {
            BeginTime = DateTime.Now.ToCstTime(),
            EndTime = DateTime.Now.ToCstTime(),
        };

    private bool isReady = false;
    private List<string> EntryGameItems { get; set; } = new List<string>();
    private List<string> EntryRoleItems { get; set; } = new List<string>();
    private List<string> EntryGroupItems { get; set; } = new List<string>();
    private List<string> EntryStaffItems { get; set; } = new List<string>();
    private List<string> PeripheryItems { get; set; } = new List<string>();
    private List<string> ArticleItems { get; set; } = new List<string>();

    private bool IsOnlyNeedPublishTime { get; set; } = true;
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                //获取所有词条
                EntryGameItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/0");
                EntryRoleItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/1");
                EntryGroupItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/2");
                EntryStaffItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/3");
                //获取所有文章
                ArticleItems = await _httpService.GetAsync<List<string>>("api/articles/GetAllArticles");

                PeripheryItems = await _httpService.GetAsync<List<string>>("api/peripheries/GetAllPeripheries");

                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取所有词条名称列表失败");
            }
        }
    }

    private async Task OnSubmit()
    {
        try
        {
            var obj = await _httpService.PostAsync<EditVoteModel, Result>("api/votes/CreateVote", Model);
            //判断结果
            if (obj.Successful == false)
            {

                await PopupService.ToastErrorAsync("创建投票失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync("创建投票成功", "创建投票成功");
                NavigationManager.NavigateTo("/votes/index/" + obj.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "创建投票失败");
        }
    }
}
